#!/usr/bin/env python3


import sys

sys.path.insert(0, '../')
print(sys.path)

import os

if not os.getenv('DJANGO_SETTINGS_MODULE'):
    os.environ['DJANGO_SETTINGS_MODULE'] = 'mlh.settings.dev'

import django

django.setup()

from collections import OrderedDict
from django.conf import settings
from django.template import loader
import os
import time

from answers.models import QuesModel, QuesLabelModel
from answers.serializers import QuestCommentSerializer, LabelQuestSerializer


def generate_static_answers_detail_html(ques_id):
    print('%s:generate_static_answers_detail_html' % time.ctime())
    ser = QuestCommentSerializer(QuesModel.objects.get(id=ques_id))
    print(ser.data)
    a = ser.data

    label_id = a['label']['id']
    similarques = LabelQuestSerializer(QuesLabelModel.objects.filter(label_id=label_id)[:5], many=True)

    context = {
        'ques_data': ser.data,
        'label_data': similarques.data

    }
    template = loader.get_template('answers/qa-detail.html')
    html_text = template.render(context)
    file_path = os.path.join(settings.GENERATED_STATIC_HTML_FILES_DIR, 'answers_' + str(ques_id) + '.html')
    with open(file_path, 'w') as f:
        f.write(html_text)


if __name__ == '__main__':
    question_set = QuesModel.objects.all()
    for ques in question_set:
        print(ques.id)
        generate_static_answers_detail_html(ques.id)
